using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public enum GUIDepth
{
	GUI_LOADING			= 10
,	GUI_SCORE			= 20
,	GUI_PAUSE 			= 30
,	GUI_CUSTOMIZATION	= 35	
,	GUI_HUD				= 40
,	GUI_MAINMENUPAGE	= 50
}

public enum MenuPageDepth
{
	MENU_LAYER_0		= 0
,	MENU_LAYER_1		= 1
,	MENU_LAYER_2		= 2
,	MENU_LAYER_3		= 3
,	MENU_LAYER_4		= 4
}

public enum HitLayer
{
	LAYER_NONE = -1
,	LAYER_DUMMY
,	LAYER_ALL
}

public enum AnimationLabel
{
	ANIM_NONE = -1

,	ANIM_DUMMY

,	ANIM_COUNT
};

public enum StringLabel
{
	LABEL_NONE = -1
	
,	LABEL_TAG_GAMEMANAGER
,	LABEL_TAG_SCENEMANAGER
,	LABEL_TAG_PLAYER
//,	LABEL_TAG_MINIGAMEMANAGER
//,	LABEL_TAG_MAINMENUMANAGER

,	LABEL_FILE_AUDIO_STATUS
,	LABEL_FILE_FIRSTPLAY_STATUS

// dragonslayer
, 	LABEL_ROBELLO_1
, 	LABEL_ROBELLO_2
,	LABEL_DRAGON_1
,	LABEL_DRAGON_2
,	LABEL_DRAGON_3
,	LABEL_ANDY_1
	
,	LABEL_COUNT
};

public enum SFXId
{
	SFX_NONE = -1

,	SFX_DUMMY
	
,	SFX_COUNT
};

public enum MusicId
{
	MUSIC_NONE = -1
	
,	MUSIC_DUMMY
	
,	MUSIC_COUNT
};

public enum TextureId
{
	TEXTUREID_NONE = -1
	
,	TEXTUREID_DUMMY

,	TEXTUREID_COUNT
};

public enum ResolutionID
{
	RES_NONE = -1
,	RES_FULLHD
,	RES_HD_16_9
,	RES_HD_16_10
,	RES_COUNT
}


public class BM_Common
{
	public class ResolutionInfo
	{
		public string		m_strDescription = "";
		public Vector2		m_vResolution = Vector2.zero;
		public float		m_fMenuCameraFOV = 0.0f;
		
		public ResolutionInfo(string strDescription, Vector2 vResolution, float fMenuCameraFOV)
		{
			m_strDescription = strDescription;
			m_vResolution = vResolution;
			m_fMenuCameraFOV = fMenuCameraFOV;
		}
	}
	
	#region AnimationLabel	
	
	private static readonly Dictionary<AnimationLabel, string> m_sdictAnimations = new Dictionary<AnimationLabel, string>
	{
		{ AnimationLabel.ANIM_DUMMY,				"anim_dummy" 			}			
	};
	
	public static string GetAnimationName(AnimationLabel eStringName)
	{
		string strRVal = "";
		
		if(!m_sdictAnimations.TryGetValue(eStringName, out strRVal))
			strRVal = "BM_Common.GetAnimationName() -> ###################### ANIMATION NOT FOUND!";
		
		return strRVal;
	}
	
	#endregion
	
	
	#region StringLabel
	
	private static readonly Dictionary<StringLabel, string> m_sdictLabels = new Dictionary<StringLabel, string>
	{
		{ StringLabel.LABEL_TAG_GAMEMANAGER,				"Manager_Game" }
	,	{ StringLabel.LABEL_TAG_SCENEMANAGER,				"Manager_Scene" }
	,	{ StringLabel.LABEL_TAG_PLAYER,						"Player" }

//	,	{ StringLabel.LABEL_TAG_MINIGAMEMANAGER,					"Manager_Minigame" }
//	,	{ StringLabel.LABEL_TAG_MAINMENUMANAGER,					"Manager_MainMenu" }	

		,	{ StringLabel.LABEL_ROBELLO_1,						"Oh no! The dragon kidnapped Andy!" }
		,	{ StringLabel.LABEL_ROBELLO_2,						"Only an hero like you can save Andy!\nTo deal with the dragon you need a helm, a sword and a shield! " }
		,	{ StringLabel.LABEL_DRAGON_1,						"MHUAHAHAHAHAH! You'll never know where Andy is hidden!" }
		,	{ StringLabel.LABEL_DRAGON_2,						"" }
		,	{ StringLabel.LABEL_DRAGON_3,						"Ouch! Ok ok, Andy is in the cave... ay!" }
		,	{ StringLabel.LABEL_ANDY_1,							"Oh, thanks my hero! Quick, bring me to the castle..." }

	};
	
	public static string GetLabelName(StringLabel eStringName)
	{
		string strRVal = "";
		
		if(!m_sdictLabels.TryGetValue(eStringName, out strRVal))
			strRVal = "BM_Common.GetLabelName() -> ###################### STRING NOT FOUND!";
		
		return strRVal;
	}
	
	#endregion
	
	
	#region SoundLabel
	
	public class AudioData
	{
		public readonly string		m_sourceFileName;				
		public readonly float		m_volume;				
		public readonly float		m_waitTimeBeforePlay; // definisce un tempo minimo di attesa in cui non può essere risuonato
		public float				m_lastPlayTime; 
		public readonly AudioClip	m_audioClip;
		public readonly float		m_rolloffDistance;
		
		public AudioData(string sourceString, float fVolume, float fWaitTimeBeforePlay, float fRolloffDistance)
		{
			m_sourceFileName 		= sourceString;
			m_volume 				= fVolume;
			m_waitTimeBeforePlay 	= fWaitTimeBeforePlay;
			m_lastPlayTime			= float.MinValue;
			m_audioClip 			= Resources.Load(m_sourceFileName) as AudioClip;
			m_rolloffDistance		= fRolloffDistance;
		}
	}
	
	public static string GetSoundName(SFXId eStringName)
	{
		string strRVal = "";
		
		AudioData audioData;
		if(BM_AudioData.m_sdictAudioData.TryGetValue(eStringName, out audioData))
			strRVal = audioData.m_sourceFileName;
		else
			strRVal = "GP_Common.GetSoundName() -> ###################### SOUND NOT FOUND!";
		
		return strRVal;
	}
	
	public static AudioData GetAudioInfo(SFXId eStringName)
	{
		AudioData rRVal;
		
		if(!BM_AudioData.m_sdictAudioData.TryGetValue(eStringName, out rRVal) && Debug.isDebugBuild)
			Debug.LogWarning("SV_Utils.GetAudioInfo() -> " + eStringName.ToString() + " SOUND NOT FOUND!");
		
		return rRVal;
	}
	
	#endregion
	
		
	#region MusicLabel	
	
	private static readonly Dictionary<MusicId, string> m_sdictMusics = new Dictionary<MusicId, string>
	{
		{ MusicId.MUSIC_DUMMY,					"theme_main"		}
	};
	
	
	public static string GetMusicName(MusicId eStringName)
	{
		string strRVal = "";
		
		if(!m_sdictMusics.TryGetValue(eStringName, out strRVal))
			strRVal = "GP_Common.GetMusicName() -> ###################### MUSIC NOT FOUND!";
		
		return strRVal;
	}
	
	#endregion
	
		
	#region TextureLabel	
	
	private static readonly Dictionary<TextureId, string> m_sdictTextures = new Dictionary<TextureId, string>
	{
		
		{ TextureId.TEXTUREID_DUMMY, 					"dummy" 						}
	};
	
	
	public static string GetTextureName(TextureId eStringName)
	{
		string strRVal = "";
		
		if(!m_sdictTextures.TryGetValue(eStringName, out strRVal))
			strRVal = "GP_Common.GetTextureName() -> ###################### TEXTURE NOT FOUND!";
		
		return strRVal;
	}
	
	#endregion
	
	
	#region ResolutionInfo
	
	private static Dictionary<ResolutionID, ResolutionInfo> m_dictPlatformResolutions = new Dictionary<ResolutionID, ResolutionInfo>
	{
		// Galaxy S4
		{ ResolutionID.RES_FULLHD, new ResolutionInfo("FullHD",	new Vector2(1920.0f, 1080.0f), 20.0f) 	} 	
		
		// Note 2, Galaxy S3, Nexus (720x1280), iPad vecchio (768x1024)
	,	{ ResolutionID.RES_HD_16_9, new ResolutionInfo( "HD", new Vector2(1280.0f, 720.0f), 20.0f) 	} 	
		
		// Note 1 e Note Tab (800x1280), iPad vecchio (768x1024)
	,	{ ResolutionID.RES_HD_16_10, new ResolutionInfo( "HD", new Vector2(1280.0f, 800.0f), 22.2f) 	} 	
//		
//		// iPad 3		
//	,	{ "QXGA",	new Vector2(1536.0f, 2048.0f) 	} 
//		
//		// iPhone 4 (640x960), iPhone 5 (640x1136), Galaxy Tab 7" (600x1024)
//	,	{ "DVGA",	new Vector2(640.0f, 960.0f) 	} 
//		
//		// Test in Unity (456x730), Galaxy S2 (480x800)
//	,	{ "WVGA",	new Vector2(480.0f, 800.0f) 	} 
//		
//		// iPhone 3			
//	,	{ "HVGA",	new Vector2(320.0f, 480.0f) 	} 
	};
	
	public static ResolutionID GetNearestResolutionFromShortSide()
	{
		ResolutionID eRVal = ResolutionID.RES_NONE;
		float fScreenWidthMinDistance = float.MaxValue;
		
		foreach (KeyValuePair<ResolutionID, ResolutionInfo> currentPlatform in m_dictPlatformResolutions)
		{
			float fDistance = 0.0f;
			if(BM_Utils.DeviceOrientationType == BM_Utils.DeviceOrientation.ORIENTATION_LANDSCAPE)
				fDistance = Mathf.Abs(BM_Utils.ScreenHeight-currentPlatform.Value.m_vResolution.y);
			else
				fDistance = Mathf.Abs(BM_Utils.ScreenWidth-currentPlatform.Value.m_vResolution.x);
			
			if(fDistance <= fScreenWidthMinDistance)
			{
				fScreenWidthMinDistance = fDistance;
				eRVal = currentPlatform.Key;
			}
		}
		
		return eRVal;
	}
	
	public static ResolutionID GetNearestResolutionFromAspectRatio()
	{
		ResolutionID eRVal = ResolutionID.RES_NONE;
		float fAspectRatioDistance = float.MaxValue;
		
		foreach (KeyValuePair<ResolutionID, ResolutionInfo> currentPlatform in m_dictPlatformResolutions)
		{
			Vector2 vResolution = currentPlatform.Value.m_vResolution;
			float fRefAspectRatio = (BM_Utils.DeviceOrientationType == BM_Utils.DeviceOrientation.ORIENTATION_LANDSCAPE) ? vResolution.x/vResolution.y : vResolution.y/vResolution.x;
			float fDistance = Mathf.Abs(BM_Utils.ActualAspectRatio - fRefAspectRatio);
			if(fDistance < fAspectRatioDistance)
			{
				fAspectRatioDistance = fDistance;
				eRVal = currentPlatform.Key;
			}
		}
		
		return eRVal;
	}
	
	public static string GetRefResolutionName(ResolutionID eResolutionID)
	{
		ResolutionInfo rVal = null;
		
		if(!m_dictPlatformResolutions.TryGetValue(eResolutionID, out rVal))
			return "GP_Common.GetRefResolutionName() -> ###################### STRING NOT FOUND!";
		
		return rVal.m_strDescription;
	}
	
	public static float GetRefCameraFOV(ResolutionID eResolutionID)
	{
		ResolutionInfo rVal = null;
		
		if(!m_dictPlatformResolutions.TryGetValue(eResolutionID, out rVal))
			return -1.0f;
		
		return rVal.m_fMenuCameraFOV;
	}
	
	#endregion
	
	
	public static int GetLayerMaskFromLayerName(HitLayer eLayerName)
	{
		int iRVal = 0;
		
		switch(eLayerName)
		{
		case HitLayer.LAYER_DUMMY:
			iRVal = 1 << 8;
			break;
			
		case HitLayer.LAYER_ALL:
			iRVal = int.MaxValue;
			break;
			
		default:
			break;
		}
		
		return iRVal;
	}
}
